package com.web.servlet;

import com.entity.Member;
import com.result.QueryResult;
import com.result.Result;
import com.result.ResultUtils;
import com.service.MemberService;
import com.service.impl.MemberServiceImpl;
import com.util.BeanUtil;
import com.util.JsonUtils;
import com.util.ResponseUtils;
import com.util.WebUtils;
import com.vo.QueryVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "MemberServlet",urlPatterns = "/member/*")
public class MemberServlet extends HttpServlet {
    private MemberService memberService=new MemberServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {

            String uri = req.getRequestURI();
            String methodName = uri.substring(uri.lastIndexOf("/") + 1);
            if (methodName.equals("list")) {
                list(req, resp);
            }

            if (methodName.equals("add")) {
                add(req, resp);
            }

            if (methodName.equals("update")) {
                update(req, resp);
            }



            if(methodName.equals("delete")){
                delete(req,resp);
            }

            if(methodName.equals("select")){
                select(req,resp);
            }



        }catch (Exception e){
            e.printStackTrace();
            Result result = ResultUtils.buildFail(50000,"对不起，系统繁忙，待会再来!");
            ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),resp);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {


        //1.接收条件
        QueryVo queryVo = WebUtils.requestToBean(req, QueryVo.class);


        //2.执行查询   开始序号  （当前页数-1）*分页大小
        QueryResult queryResult = memberService.selectByPage(queryVo.getStartIndex(), queryVo.getLimit());


        //3.构建返回结果
        Result result = ResultUtils.buildSuccess(queryResult.getList(), queryResult.getCount());

        //转换成json，发送给前端
        //发送json给客户端
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),resp);


    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {

        //1.接收参数
        String num = req.getParameter("num");

        //2.调用service进行处理
        memberService.delete(Integer.valueOf(num));

        //3.返回删除成功的结果
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {

        //1.接收客户端的参数
        Member member = BeanUtil.createBean(req);    //WebUtils.requestToBean(req, Member.class);  拷贝总是失败


        //2.提交添加请求
        memberService.update(member);

        //3.返回添加修改的信息
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ClassNotFoundException {

        //1.接收客户端的参数
        Member member = BeanUtil.createBean(req);//WebUtils.requestToBean(req, Member.class);


        //2.提交添加请求
        memberService.add(member);

        //3.返回添加成功的信息
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);

    }

    private void select(HttpServletRequest req, HttpServletResponse resp) {

        //1.接收参数
        String name = req.getParameter("name");
        System.out.println(name);
        //2.执行查询
        QueryResult queryResult = memberService.selectByName(name);
        List<Member> list= queryResult.getList();
        list.forEach(System.out::println);

        //3.构建返回结果
        Result result = ResultUtils.buildSuccess(queryResult.getList(), queryResult.getCount());

        //转换成json，发送给前端
        //发送json给客户端
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),resp);
    }
}

